<?php

function getCurrentCatalog()
{
    return date("Y") . "/" . date("md");
}

function getResourceSavePath($catalog)
{
    return getResourceUpdatePath() . "/" . $catalog;
}

function getResourcePath($catalog, $filename)
{
    return getResourceSavePath($catalog) . "/" . $filename;
}

function getWebPath($catalog, $filename)
{
    return "resource/update/$catalog/$filename";
}

function onFileUpdate($originname, $tmp, $size, $bigfile = false)
{
    if (! isLogined()) {
        return - 3; // 未登录无权限上传
    }
    global $_G;
    // $uid = $_G["login"]["uid"];
    $uid = 1;
    if (! hasUpdatePermission($uid, $size))
        return - 2; // 无权限
    $catalog = getCurrentCatalog();
    $root = getResourceSavePath($catalog);
    if (! is_dir($root))
        mkdir($root, 0777, true); // 创建目录

    $md5 = md5_file($tmp);
    $type = substr($originname, strrpos($originname, '.') + 1);
    $filename = $md5 . ".$type";
    $path = getResourcePath($catalog, $filename);
    if (! $bigfile) {
        move_uploaded_file($tmp, $path);
    } else {
        rename($tmp, $path);
    }
    $rid = insertrecord($catalog, $filename, $originname, $uid, $tmp, $md5, $size, $type);
    if (! $rid) {
        return - 1; // databaseError
    }
    return getinformationbyrid($rid);
}

function hasUpdatePermission($uid, $size)
{
    return true;
}

function insertrecord($catalog, $filename, $originname, $uid, $tmp, $md5, $size, $type, $onlyupdatervisible = 0)
{
    $back = T("resource_record")->insert(array(
        "size" => $size,
        "md5" => $md5,
        "type" => $type,
        "filename" => $filename,
        "originname" => $originname,
        "onlyupdatervisible" => "$onlyupdatervisible",
        "uid" => $uid,
        "catalog" => $catalog
    ), array(
        "size",
        "onlyupdatervisible",
        "uid"
    ));
    if ($back)
        return T("resource_record")->getUniqueID();
    else
        return false;
}

function remove($rid)
{
    return T("resource_record")->remove_by_rid($rid);
}

function getinformationbyrid($rid)
{
    $data = T("resource_record")->fetch_by_rid($rid);
    if (! isset($data["filename"])) {
        return false;
    }
    $filename = $data["filename"];
    $catalog = $data["catalog"];
    $data["absolutpath"] = getResourcePath($catalog, $filename);
    $data["webPath"] = getWebPath($catalog, $filename);
    return $data;
}

function getResourcePathByRid($rid)
{
    $data = getinformationbyrid($rid);
    if ($data) {
        return $data["webPath"];
    }
}

function update_base64_image($uid, $data, $filename)
{
    $temppath = tempnam(sys_get_temp_dir(), "temp_");
    echo $temppath;
    if (! decode_base64($data, $temppath)) {
        return false;
    }
    $size = filesize($temppath);
    return onFileUpdate($filename, $temppath, $size);
}

function decode_base64($data, $savePath)
{
    $data = explode(',', $data);
    $file_data = base64_decode($data[1]);
    return file_put_contents($savePath, $file_data);
}

function removeResource($rid)
{
    $data = getinformationbyrid($rid);
    if (! $data) {
        return false;
    }
    if (! hasRepeat($data["md5"])) {
        $path = $data["absolutpath"];
        if (file_exists($path))
            unlink($path);
    }
    T("resource_record")->remove("`rid`='$rid'");
}

function allowUpdateBigFile($uid)
{
    if (is_Teacher($uid)) {
        return true;
    }
    return false;
}

function hasRepeat($md5)
{
    return T("resource_record")->count_by_md5($md5) >= 2;
}

function createBigFileHeader($exp, $all, $size)
{
    $uid = time();
    createBigFileTemp($uid);
    $_SESSION["s_bigFile"][$uid]["all"] = $all;
    $_SESSION["s_bigFile"][$uid]["complete"] = false;
    $_SESSION["s_bigFile"][$uid]["starttime"] = time();
    $_SESSION["s_bigFile"][$uid]["exp"] = $exp;
    $_SESSION["s_bigFile"][$uid]["nowamount"] = 0;
    $_SESSION["s_bigFile"][$uid]["size"] = $size;
    return $uid;
}

function receiveBigFileCell($tmp, $uid, $index, $orgianname)
{
    $data = $_SESSION["s_bigFile"][$uid];
    if (! $data) {
        return;
    }
    $exp = $data["exp"];
    $filename = getBigFileTempFolder($uid) . "/$uid.$exp" . "_$index";
    move_uploaded_file($tmp, $filename);
    $_SESSION["s_bigFile"][$uid]["nowamount"] = $data["nowamount"] + 1;
    $back = array();

    if ($_SESSION["s_bigFile"][$uid]["nowamount"] == $_SESSION["s_bigFile"][$uid]["all"]) {
        $callback = appendBigFile($uid);
        $callback["orgname"] = $orgianname;
        $back["data"] = $callback;
        $back["state"] = 1;
        return $back;
    }
    $back["state"] = 0;
    $back["nowamount"] = $data["nowamount"] + 1;
    $back["total"] = $data["all"];
    return $back;
}

function appendBigFile($uid)
{
    $data = $_SESSION["s_bigFile"][$uid];
    $i = 0;
    $all = $data["all"];
    $exp = $data["exp"];
    $newFile = getBigFileTempFolder($uid) . "/$uid.$exp";
    $fp = fopen($newFile, "ab");
    while ($i < $all) {
        $filename = getBigFileTempFolder($uid) . "/$uid.$exp" . "_" . ($i + 1);
        $fuck = fopen($filename, "rb");
        fwrite($fp, fread($fuck, filesize($filename)));
        fclose($fuck);
        unlink($filename);
        unset($fuck);
        $i ++;
    }
    fclose($fp);
    return onFileUpdate("$uid.$exp", $newFile, filesize($newFile), true);
}

function getBigFileTempFolder($uid)
{
    return "resource/update/bigfiletemp/$uid";
}

function createBigFileTemp($uid)
{
    $file = getBigFileTempFolder($uid);
    if (! is_dir($file))
        mkdir($file, 0777, true);
}
?>